package saucaca.fragment;

import java.util.List;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.MainActivity;
import lemon.kenhnhacvn.R;

import org.json.JSONException;

import saucaca.Adapter.NewsAdapter;
import saucaca.CustomView.PagerSlidingTabStrip;
import saucaca.CustomView.XListView;
import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.NewsObject;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


public class NewFragment extends Fragment implements saucaca.CustomView.XListView.IXListViewListener {
	MainActivity mActivity;
	List<NewsObject> listNew;
	XListView listview;
	private View mView_EmptyLoading = null;
	NewsAdapter adapter;
	String page = "0";
	String pageSize = "";
	int xpage = 0;
	boolean loadingMore;
	private SwipeRefreshLayout mSwipeLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		getActivity().getActionBar().hide();
		View view = inflater.inflate(R.layout.listview, container, false);
		findViewById(view);
		new GetFromServices().execute("");
		mActivity.myActionBar.setMenu(R.string.menu_tinamnhac, false, true);
		return view;
	}
	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		mActivity.myActionBar.setMenu(R.string.menu_tinamnhac, false, true);
	}
	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	public void findViewById(View view) {
		listview = (XListView) view.findViewById(R.id.layoutlistview_listview);
		listview.setPullLoadEnable(true);
		listview.setXListViewListener(this);
		listview.startProgressBar();
		mView_EmptyLoading = view.findViewById(R.id.empty_loading);
		listview.setEmptyView(mView_EmptyLoading);
		mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
		mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				loadingMore = false;
				listview.stopProgressBar();
				stop();
				new GetFromServices().execute("");
			}
		});
		mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			mActivity = (MainActivity) activity;
		}
	}

	class GetFromServices extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... params) {
			page = Integer.toString(xpage);
			loadingMore = true;
			String str = WServiceLib.newsList(page, pageSize);
			BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null) {
				try {
					if (adapter == null) {
						listNew = JsonReader.readNews_List(result);
						adapter = new NewsAdapter(getActivity(), listNew, NewFragment.class.getName());
						listview.setAdapter(adapter);
					}else{
						List<NewsObject> listNews=JsonReader.readNews_List(result);
						for (int i = 0; i < listNews.size(); i++) {
							boolean isAdded=false;
							for (int j = 0; j < listNew.size(); j++) {
								if (listNew.get(j).getAUTO_ID().equals(listNews.get(i).getAUTO_ID())) {
									isAdded=true;
								}
							}
							if(!isAdded)
								listNew.add(listNews.get(i));
						}
					}
					loadingMore = false;
					listview.stopProgressBar();
					if(mSwipeLayout.isRefreshing())
						mSwipeLayout.setRefreshing(false);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}

	}

	public void stop() {
		listview.stopRefresh();
		listview.stopLoadMore();
	}

	@Override
	public void onRefresh() {
		if (!loadingMore) {
			xpage += 1;
			new GetFromServices().execute("");
		} else
			stop();
	}

	@Override
	public void onLoadMore() {
		if (!loadingMore) {
			xpage += 1;
			new GetFromServices().execute("");
		} else
			stop();
	}
}
